package com.bailiban.base.service;

import com.bailiban.base.entity.Label;
import com.bailiban.base.mapper.LabelMapper;
import com.bailiban.common.util.IdWorker;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName LabelService
 * @Description TODO
 * @Author ThinkPad
 * @Date 2020/11/16 15:47
 * @Version 1.0
 **/
@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class LabelService {
    @Autowired
    private LabelMapper labelMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 根据ID查询label
     *
     * @param id
     * @return
     */
    public Label queryById(String id) {
        return labelMapper.selectById(id);
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<Label> findAll() {
        return labelMapper.selectList(null);
    }

    /**
     * 新增
     */
    public void save(Label label) {
        label.setId(idWorker.nextId() + "");
        labelMapper.insert(label);
    }
    /**
     *更新
     */
    public void update(Label label){
        labelMapper.updateById(label);
    }
    /**
     * 根据ID删除
     */
    public void delete(String id){
        labelMapper.deleteById(id);
    }
    /**
     * 带条件分页查询
     */
    public Page search(Map map,int page, int size){
        QueryWrapper<Label> queryWrapper = new QueryWrapper<>();
        Set<String> set = map.keySet();
        for (String s : set){
            queryWrapper.eq(null != map.get(s),s,map.get(s));
        }
        Page<Label> page1 = new Page<Label>(page, size);
        Page<Label> labelPage = labelMapper.selectPage(page1, queryWrapper);
        return labelPage;
    }
}
